package org.gruposp2p.dnie.client.server;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;

public class ServerFacade {

    public static void doPost(
                    String url, String postData, RequestCallback requestCallback) {
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
        Log.debug("doPost : " + postData);
        builder.setHeader("Content-Type", "application/json");
        try {
            Request response = builder.sendRequest(postData, requestCallback);
        } catch (RequestException e) {
            Log.error(e.getMessage(), e);
        }
    }

    public static void doGet(String url, RequestCallback requestCallback) {
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
        Log.debug("doGet: " + url);
        try {
            Request response = builder.sendRequest("", requestCallback);
        } catch (RequestException e) {
            Log.error(e.getMessage(), e);
        }
    }

    public static void doPut(
            String url, String putData, RequestCallback requestCallback) {
        Log.debug("doPut: " + url);
        RequestBuilderExtended builder = new RequestBuilderExtended(
                RequestBuilderExtended.PUT, url);
        try {
            Request response = builder.sendRequest(putData, requestCallback);
        } catch (RequestException e) {
            Log.error(e.getMessage(), e);
        }
    }

    public static void doDelete(String url, RequestCallback requestCallback) {
        Log.debug("doDelete: " + url);
        RequestBuilderExtended builder = new RequestBuilderExtended(
                RequestBuilderExtended.DELETE, url);
        try {
            Request response = builder.sendRequest("", requestCallback);
        } catch (RequestException e) {
            Log.error(e.getMessage(), e);
        }
    }

}
